#ifndef INP_WEATHER_H
#define INP_WEATHER_H

#include "stdafx.h"
using namespace std;
class Inp_weather 
{
	//inp_weather contains weather files in a tempmap
	//at least	P [mm/day]
	//			T [degreeC]
	//			R [W/m2]
public:
	tempmap <string,double> *ptr;
	vector <string> variables;
	time_intervall whole_time;
	string root;
	string name;
	string suffix;
	string time_horizon;
	map<string,int> CONT;

	Inp_weather(
		map<string,int> ICONT,
		vector <string> ivariables,		//alle Variablen, die man so braucht
		string intervall_mode,			//Intervallmode
		int multipli,					//multipli
		double dur,						//duration
		string itime_horizon,			//"recent" or "longterm"
		time_intervall iwhole_time,		//Intervall for which we would need the data
		string iroot,					//directory of read/write file
		string iname,					//name of read/write file
		string isuffix					//suffix of read/write file
		)
	{
		variables=ivariables;
		CONT=ICONT;
		ptr=new tempmap <string,double> (0,-999999,intervall_mode,multipli,dur);
		time_horizon=itime_horizon;
		whole_time=iwhole_time;
		root=iroot;
		name=iname;
		suffix=isuffix;
	}
	~Inp_weather()
	{
		
	}
	int set_input(inp_tempmap_aggr *aggr,map <string,double> &plotprops)
	{
		if (CONT["READ_FROM_FILE"]!=1){
			if(set_input_from_REMO(plotprops)) return 1;	
		}
		else
		{
			if(set_input_from_file()) {
				if(set_input_from_REMO(plotprops)) return 1;
				else return 0;
			}
			else return 0;
		}
		return 0;
	}
	int set_input_from_file()
	{
		  
		if (this->ptr->set_read_from_file(root,name,suffix,
			this->ptr->intervall_mode,
			this->ptr->multipli,
			this->ptr->dur))
		{
			return 1;
		}
		return 0;
	}
	int set_input_from_REMO(map <string,double> &plotprops)
	{
		
		this->set_temporal_discretisation(whole_time.get_start(),whole_time.get_end(),
			0.0001,variables);
		if (this->set_read_weatherPTR_from_REMO_mon(variables,plotprops["lat"],plotprops["lon"])){
			err::err3("plot::set_met_mon_from_REMO:Can not generate weather data from REMO.");
			return 1;
		}
		this->ptr->print_in_file_simple_style_with_mydate(root,name,suffix);
		return 0;
	}
	int set_temporal_discretisation(timestamp begin_date,timestamp end_date,double intervallduration,vector<string> &val_id)
	{
		return (ptr->set_an_empty_tempmap(begin_date,end_date,intervallduration,val_id));
		
	}
	int set_read_weatherPTR_from_REMO_mon(vector <string> key_vect,double &lat,double &lon)
	{
		int dec;
		unsigned int row;
		unsigned int col;
		int year;
		int mon;
		timestamp date_origin;

		read_remo *remo=new read_remo("./weather/"); //Initialisation of routine; open files
		if((unsigned int)ptr->check_for_keys(key_vect)<key_vect.size()){ err::err3("inp_weather::read_weatherPTR_from_REMO_mon:; keys are not within map ptr"); return 1;}
		ptr->check_myself();
		for (col=0;col<key_vect.size();col++)
		{	if (this->time_horizon=="recent")
			{
				if(key_vect[col]=="P") dec=0;
				else if (key_vect[col]=="T") dec=1;
				else if (key_vect[col]=="R") dec=2;
				else { err::err3("Strange weather variable identification");return 1;}
			}
			if(this->time_horizon=="longterm")
			{
				if (key_vect[col]=="P") dec=3;
				else if (key_vect[col]=="T") dec=4;
				else if (key_vect[col]=="R") dec=5;
				else { err::err3("Strange weather variable identification");return 1;}
			}
			for(row=0;row<ptr->get_size();row++)
			{
				if(dec<3){
					year=ptr->get_time_intervall(row).get_start().get_year();
					mon=ptr->get_time_intervall(row).get_start().get_mon();
					if(ptr->set_val(key_vect[col],row,remo->calc_weather_remo(dec,year,mon,lat,lon))) {err::err3("Inp_weather::read_weatherPTR_from_REMO_mon: can not write Remu value");}
					if(dec==0) ptr->set_val(key_vect[col],row,ptr->get_val(key_vect[col],row)/Basics_MyDate::get_mondur(year,mon)); // P from [mm/month] into [mm/day]
				}
				else{
					year=ptr->get_time_intervall(row).get_start().get_year();
					mon=ptr->get_time_intervall(row).get_start().get_mon();
					if(ptr->set_val(key_vect[col],row,remo->calc_longterm_weather_remo(dec-3,year-30,year,mon,lat,lon))) {err::err3("Inp_weather::read_weatherPTR_from_REMO_mon: can not write Remo value");}
					if(dec==3) ptr->set_val(key_vect[col],row,ptr->get_val(key_vect[col],row)/Basics_MyDate::get_mondur(year,mon)); // P from [mm/month] into [mm/day]
				}
			}
		}
		delete remo;
		if (ptr->check_myself()) {err::err3("Inp_weather::read_weatherPTR_from_REMO_mon: destroyed data structure of weather file");
			return 1;
		}
		return 0;
	}

	double get_sum(string id, time_intervall ti)
	{
		return this->ptr->get_sum(id,ti);
	}
	double get_rate(string id, time_intervall ti) const
	{
		return this->ptr->get_rate (id,ti);
	}
};
#endif